package cn.stylefeng.guns.modular.rm;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.stylefeng.guns.base.utils.StringUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.nio.charset.Charset;
import java.util.List;

/**
 * @Description TODO
 * @Author xiesm
 * @Date 2024/4/17 9:15
 **/
@Slf4j
public class RMTest {
    public static final Integer pass = 0;
    public static void main(String[] args) {
//        String body = getGames("0", null);
//        getGame(body);
        getGameInfo2();
    }

    public static String getGameInfo2() {
        List<String> ids = FileUtil.readLines("D:\\home\\rm\\list.txt", Charset.defaultCharset());
        for (int i = 0; i < ids.size(); i++) {
            if (i < pass) {
                continue;
            }
            try {
                ThreadUtil.sleep(1000);
                String id = ids.get(i).trim();
                String body = getGames("1", id);
//                String body = FileUtil.readString("D:\\home\\rm\\body.txt", Charset.defaultCharset());
                Document document = Jsoup.parse(body);

                String time = document.getElementsByClass("time").get(0).getElementsByTag("span").text();

                String onclick = document.getElementsByClass("chatShowIcon").get(0).attr("onclick");
//                onclick = onclick.replace("chatShow(", "")
//                        .replace("'", "")
//                        .replace(")", "").trim();
//                String[] names = onclick.split(",");

                List<String> names = StringUtil.replace(onclick, "chatShow");

                String teamName = names.get(3);
                String[] teamNames = teamName.trim().split("v");

                String format = StrUtil.format(
                        "{}\t{}\t{}\t{}\t{}",
                        names.get(0).trim(),
                        time,
                        names.get(2),
                        teamNames[0].trim(),
                        teamNames[1].trim());
                System.out.println(format);

//            String onclick = document.getElementsByClass("chatShowIcon").get(0)
                JSONObject chatShow = getShowInfo(document.getElementsByClass("chatShowIcon").get(0));

                Elements contents = document.getElementsByClass("content-card");
                Element element = contents.get(0);
                Elements rows = element.getElementsByClass("content_row");

                for (int j = 0; j < rows.size(); j++) {
                    Element row = rows.get(j);
                    String table_option = row.getElementsByClass("table_option").text();
                    String table_rate = row.getElementsByClass("table_rate").text();
                    String money = chatShow.getString(table_option.replace(" ", ""));
                    format = StrUtil.format("{}\t{}\t{}", table_option, table_rate, money);
                    if (StrUtil.isNotEmpty(table_option)) {
                        System.out.println(format);
                    }
//                jsonObject.put(table_option,)
                }
            } catch (Exception e) {
            }
        }
        return null;
    }

    public static String getGameInfo() {
        List<String> ids = FileUtil.readLines("D:\\home\\rm\\list2.txt", Charset.defaultCharset());
        for (int i = 0; i < ids.size(); i++) {
            String id = ids.get(i).trim();
            String body = getGames("1", id);
            Document document = Jsoup.parse(body);
            //System.out.println(body);

            String onclick = document.getElementsByClass("chatShowIcon").get(0).attr("onclick");
            onclick = onclick.replace("chatShow(", "")
                    .replace("'", "")
                    .replace(")", "").trim();
            String[] names = onclick.split(",");

            String teamName = names[3];
            String[] teamNames = teamName.trim().split("v");

            String format = StrUtil.format("{}\t{}\t{}\t{}", names[0], names[2], teamNames[0], teamNames[1]);
            System.out.println(format);

            Elements contents = document.getElementsByClass("content-card");
            Element element = contents.get(0);
            Elements rows = element.getElementsByClass("content_row");
            for (int j = 0; j < rows.size(); j++) {
                Element row = rows.get(j);
                String table_option = row.getElementsByClass("table_option").text();
                String table_rate = row.getElementsByClass("table_rate").text();
                format = StrUtil.format("{}\t{}", table_option, table_rate);
                System.out.println(format);
            }

        }
        return null;
    }

    public static String getGame(String data) {
        Document document = Jsoup.parse(data);
        Elements gameList = document.getElementsByClass("game_list");
        Element game = gameList.get(0);
        Elements lis = game.select("li");

        String text = "{}\t{}\t{}\t{}\t{}";

        for (int i = 0; i < lis.size(); i++) {
            Element li = lis.get(i);
            String onclick = li.attr("onclick");
            String gameid = onclick.substring(onclick.indexOf("'") + 1, onclick.lastIndexOf("'"));
            Elements gameTeams = li.getElementsByClass("game_team");
            String team1 = gameTeams.get(0).text();
            String team2 = gameTeams.get(1).text();

            String gameName = li.getElementsByClass("game_name").text();
            Integer num = gameName.lastIndexOf(" ");

            String time = gameName.substring(0, num);
            String name = gameName.substring(num);
            System.out.println(StrUtil.format(text, gameid, name, team1, team2, time));
        }
        return null;
    }

    public static String getGames(String type, String gameid) {
        JSONObject params = new JSONObject();
        params.put("type", type);
        String url = null;
        switch (type) {
            case "0":
                url = "http://124.222.75.100:8089/cp/rmmarket?type={}";
                url = StrUtil.format(url, type);
                break;
            case "1":
                url = "http://124.222.75.100:8089/cp/rmmarket?type={}&gameid={}";
                url = StrUtil.format(url, type, gameid);
                break;
        }

        String body = HttpUtil.createGet(url)
                .execute().body();
        return body;
    }

    public static JSONObject getShowInfo(Element element) {
        String onclick = element.attr("onclick")
                .replace("chatShow(", "")
                .replace("'", "")
                .replace(")", "").trim();
        String[] names = onclick.split(",");
        return getShowInfo(names[0], names[1], names[2], names[3]);
    }

    public static JSONObject getShowInfo(String gameId, String marketId, String competitionName, String gameName) {
        String show = getShow(gameId, marketId, competitionName, gameName);
        Document document = Jsoup.parse(show);
        String[] strs = document.select("script").get(0).data().split("\\n\\t");
        JSONObject res = new JSONObject();
        for (int i = 0; i < strs.length; i++) {
            String str = strs[i];
            if (str.indexOf("var st =") != -1) {
                String[] sts = str.substring(str.indexOf("[") + 1, str.indexOf("]")).split(",");
                String str2 = strs[i + 1];
                String[] stCharts = str2.substring(str2.indexOf("[") + 1, str2.indexOf("]")).split(",");
                for (int j = 0; j < stCharts.length - 2; j++) {
                    res.put(stCharts[j].replace(" ", "").replace("'", ""), sts[j]);
                }
            }

        }
        return res;
    }

    public static String getShow(String gameId, String marketId, String competitionName, String gameName) {
        String url = "http://124.222.75.100:8089/cp/chatShow";
        String body = HttpUtil.createGet(url)
                .form("gameId", gameId)
                .form("marketId", marketId)
                .form("competitionName", competitionName)
                .form("gameName", gameName)
                .execute().body();
        return body;
    }
}
